#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution 
{
    TreeNode * createNode(int val)
    {
        return new TreeNode(val);
    }
    TreeNode * recursion(TreeNode * node1, TreeNode * node2)
    {
        TreeNode * result;
        if(node1 == nullptr && node2 == nullptr)
        {
            return nullptr;
        }
        else if (node1 == nullptr && node2 != nullptr)
        {
            result = createNode(node2->val);
            result->left = recursion(nullptr, node2->left);
            result->right = recursion(nullptr, node2->right);
        }
        else if (node1 != nullptr && node2 == nullptr)
        {
            result = createNode(node1->val);
            result->left = recursion(node1->left, nullptr);
            result->right = recursion(node1->right, nullptr);
        }
        else
        {
            result = createNode(node1->val + node2->val);
            result->left = recursion(node1->left, node2->left);
            result->right = recursion(node1->right, node2->right);
        }
        return result;
    }
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) 
    {
        return recursion(root1, root2);
    }
};

// 辅助函数：从数组构建二叉树（层序遍历方式，-1表示null）
TreeNode* buildTreeFromArray(vector<int>& arr) {
    if (arr.empty() || arr[0] == -1) return nullptr;
    
    TreeNode* root = new TreeNode(arr[0]);
    queue<TreeNode*> q;
    q.push(root);
    
    int i = 1;
    while (!q.empty() && i < arr.size()) {
        TreeNode* node = q.front();
        q.pop();
        
        // 左子节点
        if (i < arr.size() && arr[i] != -1) {
            node->left = new TreeNode(arr[i]);
            q.push(node->left);
        }
        i++;
        
        // 右子节点
        if (i < arr.size() && arr[i] != -1) {
            node->right = new TreeNode(arr[i]);
            q.push(node->right);
        }
        i++;
    }
    
    return root;
}

// 辅助函数：层序遍历打印二叉树
void printTree(TreeNode* root) {
    if (!root) {
        cout << "[]" << endl;
        return;
    }
    
    queue<TreeNode*> q;
    q.push(root);
    vector<string> result;
    
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        
        if (node) {
            result.push_back(to_string(node->val));
            q.push(node->left);
            q.push(node->right);
        } else {
            result.push_back("null");
        }
    }
    
    // 移除末尾的null
    while (!result.empty() && result.back() == "null") {
        result.pop_back();
    }
    
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (!root) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    cout << "=== LeetCode 617. 合并二叉树 测试案例 ===" << endl << endl;
    
    // 测试案例1：题目示例1
    cout << "测试案例1：" << endl;
    vector<int> tree1_1 = {1, 3, 2, 5, -1, -1, -1};
    vector<int> tree2_1 = {2, 1, 3, -1, 4, -1, 7};
    TreeNode* root1_1 = buildTreeFromArray(tree1_1);
    TreeNode* root2_1 = buildTreeFromArray(tree2_1);
    
    cout << "输入: root1 = ";
    printTree(root1_1);
    cout << "      root2 = ";
    printTree(root2_1);
    
    TreeNode* result1 = solution.mergeTrees(root1_1, root2_1);
    cout << "输出: ";
    printTree(result1);
    cout << "期望: [3,4,5,5,4,null,7]" << endl << endl;
    
    // 测试案例2：题目示例2
    cout << "测试案例2：" << endl;
    vector<int> tree1_2 = {1};
    vector<int> tree2_2 = {1, 2};
    TreeNode* root1_2 = buildTreeFromArray(tree1_2);
    TreeNode* root2_2 = buildTreeFromArray(tree2_2);
    
    cout << "输入: root1 = ";
    printTree(root1_2);
    cout << "      root2 = ";
    printTree(root2_2);
    
    TreeNode* result2 = solution.mergeTrees(root1_2, root2_2);
    cout << "输出: ";
    printTree(result2);
    cout << "期望: [2,2]" << endl << endl;
    
    // 测试案例3：一个树为空
    cout << "测试案例3（一个树为空）：" << endl;
    vector<int> tree1_3 = {};
    vector<int> tree2_3 = {1, 2, 3};
    TreeNode* root1_3 = buildTreeFromArray(tree1_3);
    TreeNode* root2_3 = buildTreeFromArray(tree2_3);
    
    cout << "输入: root1 = ";
    printTree(root1_3);
    cout << "      root2 = ";
    printTree(root2_3);
    
    TreeNode* result3 = solution.mergeTrees(root1_3, root2_3);
    cout << "输出: ";
    printTree(result3);
    cout << "期望: [1,2,3]" << endl << endl;
    
    // 测试案例4：两个树都为空
    cout << "测试案例4（两个树都为空）：" << endl;
    TreeNode* root1_4 = nullptr;
    TreeNode* root2_4 = nullptr;
    
    cout << "输入: root1 = ";
    printTree(root1_4);
    cout << "      root2 = ";
    printTree(root2_4);
    
    TreeNode* result4 = solution.mergeTrees(root1_4, root2_4);
    cout << "输出: ";
    printTree(result4);
    cout << "期望: []" << endl << endl;
    
    // 测试案例5：不同结构的树
    cout << "测试案例5（不同结构）：" << endl;
    vector<int> tree1_5 = {1, 2, -1, 3};
    vector<int> tree2_5 = {1, -1, 2, -1, -1, 3};
    TreeNode* root1_5 = buildTreeFromArray(tree1_5);
    TreeNode* root2_5 = buildTreeFromArray(tree2_5);
    
    cout << "输入: root1 = ";
    printTree(root1_5);
    cout << "      root2 = ";
    printTree(root2_5);
    
    TreeNode* result5 = solution.mergeTrees(root1_5, root2_5);
    cout << "输出: ";
    printTree(result5);
    cout << "期望: [2,2,2,3,null,3]" << endl << endl;
    
    // 清理内存
    deleteTree(result1);
    deleteTree(result2);
    deleteTree(result3);
    deleteTree(result4);
    deleteTree(result5);
    
    cout << "所有测试案例执行完毕！" << endl;
    
    return 0;
}